﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OsuTool
{
    class RemoveLNConverter
    {
        private ConvertNoLnState _convertNoLnState = ConvertNoLnState.None;

        public ConvertNoLnMode ConvertMode { get; set; }

        public RemoveLNConverter(ConvertNoLnMode mode)
        {
            ConvertMode = mode;
        }

        public RemoveLNConverter()
        {
            ConvertMode = ConvertNoLnMode.T1;
        }

        public void ConvertNoLn(string file)
        {
            _convertNoLnState = ConvertNoLnState.ChangeVersion;
            var aimFile = $"{file.Replace(".osu", "")} [{OsuFileInfo.StrNoLn}].osu";
            var fs = !File.Exists(aimFile)
                ? new FileStream(aimFile, FileMode.Create, FileAccess.Write)
                : new FileStream(aimFile, FileMode.Open, FileAccess.Write);

            var writer = new StreamWriter(fs);
            using (var reader = new StreamReader(file))
            {
                var line = reader.ReadLine();
                while (line != null)
                {
                    if (_convertNoLnState == ConvertNoLnState.ChangeVersion)
                    {
                        if (line.Contains(OsuFileInfo.StrVersion))
                        {
                            line = $"{line} [{OsuFileInfo.StrNoLn}]";
                            _convertNoLnState = ConvertNoLnState.FindHitObjects;
                            writer.WriteLine(line);
                            line = reader.ReadLine();
                            continue;
                        }

                        writer.WriteLine(line);
                    }

                    if (_convertNoLnState == ConvertNoLnState.FindHitObjects)
                    {
                        if (line.Contains(OsuFileInfo.StrHitObjects))
                        {
                            _convertNoLnState = ConvertNoLnState.Convert;
                            writer.WriteLine(OsuFileInfo.StrHitObjects);
                            line = reader.ReadLine();
                            continue;
                        }

                        writer.WriteLine(line);
                    }

                    if (_convertNoLnState == ConvertNoLnState.Convert)
                    {
                        writer.WriteLine(GetNoLnStr(line));
                    }

                    line = reader.ReadLine();
                }

                writer.Close();
                writer.Dispose();
            }
        }

        private string GetNoLnStr(string oldHitObjectStr)
        {
            var s1 = oldHitObjectStr.Split(',');
            var s2 = s1[5].Split(':');
            var result = $"{s1[0]},{s2[1]},{s1[2]},1,0,0:0:0:0:";
            return result;
        }

        public enum ConvertNoLnState
        {
            ChangeVersion,
            FindHitObjects,
            Convert,
            None
        }

        public enum ConvertNoLnMode
        {
            T1,
            T2,
            T3,
            T4,
            T5,
            T6,
            T7
        }
    }
}